package com.eascs.spider.kuaixiao.manager;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eascs.spider.kuaixiao.spider.ShunqiwangCrawler;
import com.eascs.spider.kuaixiao.util.RedisUtil;

public class TaskManager {
	private static Logger logger = LoggerFactory.getLogger(TaskManager.class);
	
	private static Queue<String> queue = new ConcurrentLinkedQueue<>();
	private static ExecutorService executors;
	private static TaskManager instance;
	
	private static TaskManager init(){
		executors = Executors.newFixedThreadPool(50);
		Schdule schdule = new Schdule();
		schdule.start();
		instance = new TaskManager();
		logger.info("任务管理器初始化完毕");
		return instance;
	}
	
	public synchronized static TaskManager getInstance(){
		return instance==null?init():instance;
	}
	
	//添加请求url到队列
	public void addUrl(String url){
		queue.add(url);
	}
	
	static class Schdule extends Thread{
		
		@Override
		public void run(){
			while(true){
				try {
					if(!queue.isEmpty()){
						String url = queue.poll();
						if(!RedisUtil.existShunqiwang(url)){
							ShunqiwangCrawler crawler = new ShunqiwangCrawler(url);
							executors.execute(crawler);
							Thread.sleep(2000);
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}
}
